<template>
  <div class="routerPage">
    <ul>
      <li v-for="item in newsList" :key="item.id" @click="go(item.url)">
        <div><img :src="item.picUrl" alt="" /></div>
        <p class="p1">{{ item.title }}</p>
        <p class="p2">{{ item.description }}</p>
        <p class="p3">
          <span>{{ item.ctime }}</span
          ><span>{{ item.source }}</span>
        </p>
      </li>
    </ul>
    <div class="bottom">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="50"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
//引入配饰好的api接口
import { getdata } from "../../request/api";

export default {
  data() {
    return {
      newsList: [],
      currentPage: 1,
    };
  },
  props: ["url"],
  methods: {
    go(url) {
      //跳转到外部页面
      window.location.href = url;
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      getdata(this.url, {
        key: "a9f2c38f3ecab03a55bd017041f75f4d",
        num: 12,
        page: this.currentPage,
      }).then((res) => {
        console.log(res);
        this.newsList = res.newslist;
      });
    },
  },
  created() {
    getdata(this.url, {
      key: "a9f2c38f3ecab03a55bd017041f75f4d",
      num: 12,
      page: this.currentPage,
    }).then((res) => {
      console.log(res);
      this.newsList = res.newslist;
    });
  },
  watch: {
    $route() {
      this.$router.go(0);
    },
  },
};
</script>

<style scoped>
.routerPage {
  width: 1000px;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
}
ul li {
  width: 300px;
  height: 350px;

  display: flex;
  padding: 10px;
  flex-direction: column;
  justify-content: space-between;
}
ul li img {
  width: 300px;
  height: 200px;
}
.p1 {
  font-weight: bold;
}
.p2 {
  font-size: 12px;
}
span {
  display: block;
  font-size: 12px;
}
.p3 {
  display: flex;
  justify-content: space-between;
}
</style>